package com.autonavi.yunda.yunji.common.utils.geo;

import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author pengmeng on 2017/5/26.
 */
@Service
public class CircleWkt {
    /**
     * Generate circle wkt by 32 points for convenient
     *
     * @param point:  circle center point
     * @param radius: radius in km
     * @return points: 33 wkt points
     */
    public static ArrayList<GeoLatLon> generateWKT(GeoLatLon point, double radius) {
        return generateWKT(point, radius, 32);
    }

    /**
     * Generate circle wkt by given number of points
     *
     * @param point:     circle center point
     * @param radius:    radius in km
     * @param numPoints: number of points to simulate the circle
     * @return points: list of wkt points
     */
    public static ArrayList<GeoLatLon> generateWKT(GeoLatLon point, double radius, int numPoints) {
        ArrayList<GeoLatLon> wkt = new ArrayList<>();
        for (int i = 0; i < numPoints; i++) {
            double angle = ((double) i / (double) numPoints) * 360;
            double dlon = (radius * Math.sin(Math.PI * angle / 180)) / (111 * Math.cos(point.lat * Math.PI / 180));
            double dlat = (radius * Math.cos(Math.PI * angle / 180)) / 111;
            wkt.add(new GeoLatLon(point.lat + dlat, point.lon + dlon));

        }
        wkt.add(wkt.get(0));
        return wkt;
    }
}
